package com.liliang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liliang.entity.*;
import com.liliang.service.IEmployeeService;
import com.liliang.service.IIdMappingService;
import com.liliang.service.IOrdersService;
import com.liliang.service.ISupplierService;
import com.liliang.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liliang
 * @since 2021-11-04
 */
@Controller
@RequestMapping("/orders")
public class OrdersController {


    //计划表
    @Autowired
    IOrdersService ios;

    //用户信息表
    @Autowired
    IEmployeeService ies;

    //对照表
    @Autowired
    IIdMappingService iims;

    //供应商表
    @Autowired
    ISupplierService iss;

    //添加需求计划
    @PostMapping("/insert")
    @ResponseBody
    public boolean insert(Orders orders, String jhq, HttpSession session) throws IOException {
        //得到用户信息
        SysUsers su= (SysUsers) session.getAttribute("users");
//        Employee el=ies.getBaseMapper().selectOne(new QueryWrapper<Employee>().eq("USER_ID", su.getId()));
        long a=2;
        Employee el=ies.getBaseMapper().selectOne(new QueryWrapper<Employee>().eq("USER_ID", a));

        //添加到对象
        orders.setAuthorId(el.getEmpNum());
        orders.setAuthor(el.getEmpName());
        orders.setPhone(el.getPhone());
        orders.setFax(el.getFax());
        orders.setEmail(el.getEmail());
        //添加计划表
        if (ios.insert(orders, jhq)){
            RandomUtil.setliushuihao("xqjh");
            //添加对照表
            IdMapping idMapping=new IdMapping();
            idMapping.setOrderId(orders.getId());
            idMapping.setStatus("C001-10");
            return iims.save(idMapping);
        }
        return false;
//        return ios.save(orders);
    }


    //查找所有需求计划
    @ResponseBody
    @GetMapping("/findMaterialPage")
    public MaterialPage findMaterialPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows, Orders orders,String zt){
        return ios.findMaterialPage((page-1)*rows,rows,orders,zt);
    }

    //查找单个需求计划
    @ResponseBody
    @GetMapping("/findbyid")
    public Orders findMaterialPage(Integer id){
        return ios.getById(id);
    }


    //修改单个需求计划
    @ResponseBody
    @PostMapping("/insetOrders")
    public boolean insetOrders(Orders orders,String jhq){
        //设置时间戳
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //替换字符串
        jhq=jhq.replace("T"," ")+":00";
        //设置计划截止时间
        orders.setStartDate(LocalDateTime.parse(jhq,df));
        return ios.updateById(orders);
    }

    //删除单个需求计划
    @ResponseBody
    @GetMapping("/deletebyID")
    public boolean insetOrders(long OrdersId){
        boolean b = ios.removeById(OrdersId);
        if (b){
            return iims.remove(new QueryWrapper<IdMapping>().eq("ORDER_ID",OrdersId));
        }
        return false;
    }


    //查找状态为 需要采购计划的 所有需求计划 (可以排序)
    @ResponseBody
    @GetMapping("/findMaterialPage2")
    public MaterialPage findMaterialPage2(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer rows,String sort,String order,Orders orders){
        return ios.findMaterialPage2((page-1)*rows,rows,sort,order,orders);
    }


    //采购计划编制 ， 多个相同的一起编制
    @PostMapping("/next")
    public String next(HttpServletRequest request, Model model){
        //查询所有
        String[] nums = request.getParameterValues("nums");
        List<Orders> orders=new ArrayList<Orders>();
        for (int i = 0; i < nums.length ; i++) {
            orders.add(ios.getById(nums[i]));
        }
        model.addAttribute("orders",orders);
        model.addAttribute("cgbzsj",LocalDateTime.now());
        return "planman/STOCK_inset";
    }

    //采购计划编制 ， 多个相同的一起编制
    @PostMapping("/updateByorders")
    @ResponseBody
    public boolean updateByorders(Orders order){
        return ios.updateById(order);
    }

}

